package com.arpa.ntocc.common.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.cache.AreaCache;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.domain.dto.AreaCountryDTO;
import com.arpa.ntocc.common.domain.entity.AreaCountry;
import com.arpa.ntocc.common.domain.vo.AreaCountryProvinceVO;
import com.arpa.ntocc.common.domain.vo.AreaCountryVO;
import com.arpa.ntocc.common.mapper.AreaCountryMapper;
import com.arpa.ntocc.common.service.IAreaCountryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 省份信息表 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2019-12-04
 */
@Service
public class AreaCountryServiceImpl extends ServiceImpl<AreaCountryMapper, AreaCountry> implements IAreaCountryService {

    @Autowired
    private AreaCityServiceImpl areaCityServiceImpl;
    @Autowired
    private AreaCountyServiceImpl areaCountyServiceImpl;
    @Autowired
    private AreaCache areaCache;

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(AreaCountry entity) {
        if(StringUtils.isBlank(entity.getCountryCode())){
            entity.setCountryCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
        }
        return super.save(entity);
    }

    /**
     * 根据code更新实体
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public int updateByCode(AreaCountry entity) {
        if(StringUtils.isBlank(entity.getModifiedBy())){
            entity.setModifiedBy(UserUtil.getCode());
        }
        return baseMapper.update(entity,new QueryWrapper<AreaCountry>().lambda().eq(AreaCountry::getCountryCode,entity.getCountryCode()));
    }

    /**
     * 查询列表
     * @param areaCountryDTO
     * @return
     */
    @Override
    public List<AreaCountryVO> list(AreaCountryDTO areaCountryDTO){
        List<AreaCountryVO> areaCountryVOList = baseMapper.queryList(areaCountryDTO);
        return areaCountryVOList;
    }

    /**
     * 查询列表
     * @return
     */
    @Override
    public List<AreaCountryProvinceVO> queryCountryProvinceList(){
        List<AreaCountryProvinceVO> areaCountryCityVOList = areaCache.getAllCacheList();
        return areaCountryCityVOList;
    }

    /**
     * 查询合计,包含总数
     * @param areaCountryDTO
     * @return
     */
    @Override
    public AreaCountryVO queryListSum(AreaCountryDTO areaCountryDTO){
        AreaCountryVO areaCountryVO = baseMapper.queryListSum(areaCountryDTO);
        return areaCountryVO;
    }

}
